<?php


namespace app\appapi\controller;


use alipay\aop\AopClient;
use app\common\model\PhonePoolModel;
use app\common\model\RechargeModel;
use cmf\controller\RestBaseController;
use think\Db;

class ApiController extends RestBaseController
{
    public function get_subsite_config_info()
    {
        $data = Db::name('option')->find();
        $option = json_decode($data['option_value'], true);
        $option['qrcode'] = cmf_get_image_url($option['qrcode']);
        $option['site_logo'] = cmf_get_image_url($option['site_logo']);
        $this->success('ok', $option);
    }

    public function get_storage_info()
    {

    }

    public function notify()
    {
        $config = config('app.alipay');
        cmf_log(date('Y-m-d H:i:s') . PHP_EOL . var_export($_POST) . PHP_EOL, 'pay.log');

        //验证签名
        $aop = new AopClient();
        $aop->alipayrsaPublicKey = $config['ali_public_key'];
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");

        //验签
        if ($flag) {
            //处理业务，并从$_POST中提取需要的参数内容
            if ($_POST['trade_status'] == 'TRADE_SUCCESS' || $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知
                if (!RechargeModel::pay_notify($_POST)) {
                    cmf_log("错误信息：" . RechargeModel::getErrorInfo() . date("Y-m-d H:i:s") . PHP_EOL, 'pay.log');
                    die('fail');

                }
                cmf_log("支付成功：" . date("Y-m-d H:i:s") . PHP_EOL, 'pay.log');
                die('success');

                //处理成功一定要返回 success 这7个字符组成的字符串，
                //die('success');//响应success表示业务处理成功，告知支付宝无需在异步通知

            } else {
                cmf_log("错误信息：" . $_POST['trade_status'] . date("Y-m-d H:i:s") . "\r\n", 'pay.log');
                die('fail');
            }
        } else {
            cmf_log("错误信息：签名验证失败" . $flag . date("Y-m-d H:i:s") . "\r\n", 'pay.log');
            die('fail');
        }

    }

    public function rec_phone($phone)
    {
        $max_nums = 100;
        if (!cmf_check_mobile($phone)){
            $this->error('请填写正确号码');
        }

        if (PhonePoolModel::where('phone', $phone)->find()){
            $this->error('手机号已存在');
        }

        if (PhonePoolModel::count() >= $max_nums){
            $this->error('号码池已满');
        }

        if (PhonePoolModel::create(['phone' => $phone])){
            $this->success('ok');
        }else{
            $this->error('添加失败');
        }
    }


}